package com.kailong.controller;

import com.kailong.pojo.Result;
import com.kailong.utils.InkImageUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class FileUploadController {
    @PostMapping("/upload")
    public Result<String> fileUpload(MultipartFile file) throws IOException {
        //把文件的内容存储到本地磁盘中
        String originalFilename = file.getOriginalFilename();
        //保证文件的名字是唯一的，从而防止文件覆盖
        //String fileName = UUID.randomUUID().toString()+ originalFilename.substring(originalFilename.lastIndexOf("."));
        //file.transferTo(new File("D:\\workplace\\SpringBoot\\SprintBoot-mybatis\\files\\" + fileName));
        String fileName = originalFilename.substring(originalFilename.lastIndexOf("."));
        String url = InkImageUtil.uploadImg(fileName,file.getInputStream());
        return Result.success(url);
    }
}
